package ai.summer.services.controller;

import ai.summer.services.restfeign.CartFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author shizhanfu
 */
@RestController
@RequestMapping("home")
public class HomeController {

    @Autowired
    RestTemplate restTemplate;

    /**
     * 基于ribbon访问
     * @return
     */
    @GetMapping("index")
    public String Index() {
        String rest = restTemplate.getForObject("http://cartservice/home/index", String.class);

        return "order service\r\n" + rest;
    }

    @Autowired
    CartFeign cartFeign;

    @GetMapping("feign")
    @HystrixCommand(fallbackMethod = "Test1FallBack")
    public String Feign(){
        for (int i = 0; i <100; i++) {
            cartFeign.index();
        }
        return "feign order service\r\n" + cartFeign.index();
    }

    @GetMapping("test1")
    @HystrixCommand(fallbackMethod = "Test1FallBack")
    public String Test1(){
        cartFeign.index();
        throw new RuntimeException("test1");
    }

    public String Test1FallBack(){
        System.out.println("fallback test1");
        return "error";
    }
}
